<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增数据</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui/css/layui.css" media="all">
    <style type="text/css">
        .redstar{
            color: red;
            margin-right: 5px;
        }
        .layui-field-title{
            color: #ffffff;
        }

    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form" lay-filter="tableInfo" action="" style="margin:10px 20px 0 10px">
            <input type="text" id="tableId" name="tableId" class="layui-input" style="display: none">
            <input type="text" id="modelID" name="modelID" class="layui-input" style="display: none">
            <input type="text" id="modelName" name="modelName" class="layui-input" style="display: none">
            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">
                    <i class="redstar">*</i>类型
                </label>
                <div class="layui-input-block">
                    <select class="layui-select" id="type" name="type">
                        <option value="">请选择类型</option>
                        <option value="basic">基础数据</option>
                        <option value="business">业务数据</option>
                        <option value="bargain">交易数据</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">
                    <i class="redstar">*</i>表名</label>
                <div class="layui-input-block">
                    <input type="text" id="tableCode" name="tableCode" lay-verify="require" autocomplete="off" placeholder="请输入编码"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">
                    <i class="redstar">*</i>表中文名</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="tableName" name="tableName" lay-verify="required" autocomplete="off" placeholder="请输入名称">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">
                    <i class="redstar">*</i>来源</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="source" name="source" lay-verify="required" autocomplete="off" placeholder="请输入来源">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">
                    <i class="redstar">*</i>版本</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="release" name="release" lay-verify="required" autocomplete="off" placeholder="请输入版本">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">说明</label>
                <div class="layui-input-block">
                    <textarea type="text" id="remark" name="remark" style="height: 125px" class="layui-input"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button id="yes" class="layui-btn yes" lay-submit lay-filter="submit" style="display: none">确定</button>
                </div>
            </div>
        </form>
    </div>
</div>


<script src="../lib/layui/layui.js" charset="utf-8"></script>
<script src="../js/jquery-1.9.0.js"></script>
<script type="text/javascript" src="../js/common.js"></script>

<script>
    layui.use(['form','element','table'],function () {
        var element = layui.element,
            form = layui.form,
            table = layui.table;

        /**
         * 获取初始数据
         */
        var tableId = getUrlParams('tableId'),
            modelId = getUrlParams('modelId'),
            modelName = getUrlParams('modelName'),
            type = getUrlParams('type'),
            tableCode = getUrlParams('tableCode'),
            tableName = getUrlParams('tableName'),
            release = getUrlParams('release'),
            source = getUrlParams('source'),
            remark = getUrlParams('remark');

        $(document).ready(function () {
            if (modelId !== null && modelId !== undefined) {
                form.val('tableInfo', {
                    'tableId': tableId,
                    'modelID': modelId,
                    'modelName':modelName,
                    'type':type,
                    'tableCode': tableCode,
                    'tableName': tableName,
                    'release': release,
                    // 'source':source,
                    // 'remark': remark
                });
                if(source!=='null'){
                    form.val('tableInfo',{
                        'source':source
                    });
                }

                if(remark!=='null'){
                    form.val('tableInfo',{
                        'remark':remark
                    });
                }
            } else {
                form.val('tableInfo', {
                    'modelID': modelId,
                    'modelName':modelName
                });
            }
        });


        window.addEventListener('message', function (event) {
            if (event.data === 'submit') {
                $("#yes").trigger("click");
            }
        });


        /**
         * 监听提交
         */
        form.on('submit(submit)', function (data) {
            if (data.field.tableId != null && data.field.tableId !== '') {
                /**
                 * 修改数据信息
                 */
                $.ajax({
                    type: "post",
                    url: "/baseDataManagerServer/modelTable/updateModelTable",
                    dataType: 'json',
                    cache: false,
                    contentType: "application/json",
                    data: JSON.stringify(data.field),
                    success: function (redata) {
                        if (redata.code === '0000') {
                            window.parent.postMessage('update_success-' + redata.msg, '*');
                        } else if (redata.code === '9999') {
                            window.parent.postMessage('update_error-' + redata.msg, '*');
                        }
                    },
                    error: function (redata) {
                        window.parent.postMessage('update_error', '*');
                    }
                });
            } else {
                $.ajax({
                    type: "post",
                    url: "/baseDataManagerServer/modelTable/insertModelTable",
                    dataType: 'json',
                    cache: false,
                    contentType: "application/json",
                    data: JSON.stringify(data.field),
                    success: function (redata) {
                        if (redata.code === '0000') {
                            window.parent.postMessage('success-' + redata.msg, '*');
                        } else if (redata.code === '9999') {
                            window.parent.postMessage('error-' + redata.msg, '*');
                        }
                    },
                    error: function (redata) {
                        window.parent.postMessage('error', '*');
                    }
                });
            }
            return false;
        });
    });


    window.getUrlParams = function (key) {
        //获取url参数
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
        var url = decodeURI(window.location.search)
        var r = url.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
</script>

</body>
</html>